package com.aiju.common.http.interceptor;


import com.aiju.support.aop.AInterceptor;
import com.aiju.support.aop.AInterceptorHandler;
import com.aiju.utils.L;
import com.aiju.utils.Util;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author: 佐为
 * @date: 2017-03-24 12:02
 * @version: V1.0
 * @Description: 网络拦截
 */
public class RepeatInterceptor extends AInterceptor {

    private List<String> urlList;
    private boolean interceptor;

    public RepeatInterceptor() {
        urlList = Collections.synchronizedList(new ArrayList());
    }

    @Override
    protected void addMetodName() {
        methods.add("requestPost");
        methods.add("requestGet");
    }

    @Override
    public Object before(Object proxied, Method method, Object[] args) {
        if (!Util.isEmpty(args)) {
            String url = args[0].toString();
            if (urlList.contains(url)) {
                interceptor = true;
            } else {
                interceptor = false;
                urlList.add(url);
            }
        }
        return super.before(proxied, method, args);
    }

    @Override
    protected Object interceptor(Object proxied, Method method, Object[] args) {
        String url = args[0].toString();
        if (interceptor) {
            L.d(TAG, "过滤重复请求" + url);
            return AInterceptorHandler.END;
        }
        return AInterceptorHandler.DEFAULT;
    }

    @Override
    public Object after(Object proxied, Method method, Object[] args, Object methodReturn) {
        if (!Util.isEmpty(args)) {
            String url = args[0].toString();
            urlList.remove(url);
        }
        return super.after(proxied, method, args, methodReturn);
    }
}
